package cn.com.web.Servlet;

import cn.com.domain.User;
import cn.com.service.UserService;
import cn.com.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/zhuxiaoServlet")
public class zhuxiaoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user= (User) request.getSession().getAttribute("user");
        request.getSession().removeAttribute("user");
        if (user!=null)
        {
            UserService userService=new UserServiceImpl();
            userService.delteUser(user);
            //获取Cookie删除autologin的cookie
            Cookie[] cookies = request.getCookies();
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("autologin")){
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                    break;
                }
            }
            response.getWriter().write("注销成功");
            String path = request.getContextPath();
            response.getWriter().write("<a href='"
                    +path+
                    "/login.jsp'>登录页面</a>\n");

        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
